package net.hn.hnms.biz.energy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.energy.domain.WaterPlacePrice;
import net.hn.hnms.biz.energy.mapper.WaterPlacePriceMapper;
import net.hn.hnms.biz.energy.request.WaterPlacePriceReq;
import net.hn.hnms.biz.energy.service.WaterPlacePriceService;
import net.hn.hnms.biz.energy.vo.WaterPlacePriceVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class WaterPlacePriceServiceImpl implements WaterPlacePriceService {

    @Autowired
    private WaterPlacePriceMapper waterPlacePriceMapper;


    @Override
    public int insert(WaterPlacePriceReq waterPlacePriceReq) {
        WaterPlacePrice insertObj = new WaterPlacePrice();
        BeanUtil.copyProperties(waterPlacePriceReq, insertObj);
        return waterPlacePriceMapper.insert(insertObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDeleteByIds(List<String> idList) {
        waterPlacePriceMapper.updateDeleteByIds(idList);
    }

    @Override
    public void updateById(WaterPlacePriceReq waterPlacePriceReq) {
        WaterPlacePrice updateObj = new WaterPlacePrice();
        BeanUtil.copyProperties(waterPlacePriceReq, updateObj);
        int updateCount = waterPlacePriceMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    @Override
    public WaterPlacePrice selectDetailById(String id) {
        WaterPlacePrice waterPlacePrice = waterPlacePriceMapper.selectById(id);
        if (waterPlacePrice == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return waterPlacePrice;
    }

    @Override
    public ResponsePage<WaterPlacePriceVo> selectPageList(WaterPlacePriceReq waterPlacePriceReq, PageQuery pageQuery) {
//        Page<WaterPlacePrice> page = waterPlacePriceMapper.selectPage(pageQuery.build(), buildQuery(waterPlacePriceReq).orderByDesc(WaterPlacePrice::getCreateTime));
        Page<WaterPlacePriceVo> page = waterPlacePriceMapper.selectPageList(pageQuery.build(), waterPlacePriceReq);
//        List<WaterSupplyCompanyVo> list =page.getRecords().stream().map(o ->convert(o, WaterSupplyCompanyVo.class)).collect(Collectors.toList());
        return new ResponsePage<>(page.getRecords(), page.getTotal());
    }

    @Override
    public List<WaterPlacePrice> selectList(WaterPlacePriceReq waterPlacePriceReq) {
        List<WaterPlacePrice> list = waterPlacePriceMapper.selectList(buildQuery(waterPlacePriceReq));
        return list;
    }

    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<ExecutionOther> 查询条件
     */
    private LambdaQueryWrapper<WaterPlacePrice> buildQuery(WaterPlacePriceReq req){
        return new LambdaQueryWrapper<WaterPlacePrice>()
                .like(StrUtil.isNotBlank(req.getWaterPlaceName()), WaterPlacePrice::getWaterPlaceName, req.getWaterPlaceName())
                .eq(StrUtil.isNotBlank(req.getMineCode()), WaterPlacePrice::getMineCode, req.getMineCode())
                .ge(req.getBeginDateTime() != null, WaterPlacePrice::getCreateTime, req.getBeginDateTime())
                .le(req.getEndDateTime() != null, WaterPlacePrice::getCreateTime, req.getEndDateTime());
    }

}
